/**
 * Copyright 2010-present Facebook.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sromku.simple.fb.example.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.util.Log;
import android.view.*;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.model.GraphUser;
import com.facebook.widget.ProfilePictureView;
import com.sromku.simple.fb.SimpleFacebook;
import com.sromku.simple.fb.entities.Profile;
import com.sromku.simple.fb.example.LoginActivity;
import com.sromku.simple.fb.example.MainActivity;
import com.sromku.simple.fb.example.R;
import com.sromku.simple.fb.example.utils.MyUser;
import com.sromku.simple.fb.example.utils.ProfileFacebook;
import com.sromku.simple.fb.example.utils.Utils;
import com.sromku.simple.fb.listeners.OnLogoutListener;
import com.sromku.simple.fb.listeners.OnProfileListener;

public class ProfileFragment extends Fragment {

    public static final String TAG = "ProfileFragment";

    private TextView userNameView;
    private ProfilePictureView profilePictureView;
    private OnOptionsItemSelectedListener onOptionsItemSelectedListener;
    private Button mButtonLogout;
	private SimpleFacebook mSimpleFacebook;
	//private Profile profile;
    private GraphUser pendingUpdateForUser;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		mSimpleFacebook = SimpleFacebook.getInstance();
		
		
      //  setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
      //  inflater.inflate(R.menu.options_profile, menu);
        
        
    }

  /*  @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        boolean handled = false;
        OnOptionsItemSelectedListener listener = onOptionsItemSelectedListener;
        if (listener != null) {
            handled = listener.onOptionsItemSelected(item);
        }

        if (!handled) {
            handled = super.onOptionsItemSelected(item);
        }

        return handled;
    }*/

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_profile, parent, false);

        userNameView = (TextView)v.findViewById(R.id.profileUserName);
        profilePictureView = (ProfilePictureView)v.findViewById(R.id.profilePic);

        mButtonLogout = (Button) v.findViewById(R.id.button_logout);

		setLogout();
		
		/*mSimpleFacebook.getProfile(new OnProfileListener() {

			@Override
			public void onThinking() {
			}

			@Override
			public void onException(Throwable throwable) {
			}

			@Override
			public void onFail(String reason) {
			}

			@Override
			public void onComplete(Profile response) {
				ProfileFacebook.profile=response;
			}
		});*/
		UpdateProfileView();
        if (pendingUpdateForUser != null) {
            updateViewForUser(pendingUpdateForUser);
            pendingUpdateForUser = null;
        }

        return v;
    }

    public void setOnOptionsItemSelectedListener(OnOptionsItemSelectedListener listener) {
        this.onOptionsItemSelectedListener = listener;
    }

    public void updateViewForUser(GraphUser user) {
        if (userNameView == null || profilePictureView == null || !isAdded()) {
            // Fragment not yet added to the view. So let's store which user was intended
            // for display.
            pendingUpdateForUser = user;
            return;
        }

        if (user == null) {
            profilePictureView.setProfileId(null);
            userNameView.setText("greeting_no_user");
        } else {
            profilePictureView.setProfileId(user.getId());
            userNameView.setText(
                    String.format(getString(R.string.greeting_format), user.getFirstName()));
        }
    }

    public interface OnOptionsItemSelectedListener {
        boolean onOptionsItemSelected(MenuItem item);
    }
    
    
    private void setLogout() {
		final OnLogoutListener onLogoutListener = new OnLogoutListener() {

			@Override
			public void onFail(String reason) {
				//mTextStatus.setText(reason);
				Log.w(TAG, "Failed to login");
			}

			@Override
			public void onException(Throwable throwable) {
				//mTextStatus.setText("Exception: " + throwable.getMessage());
				Log.e(TAG, "Bad thing happened", throwable);
			}

			@Override
			public void onThinking() {
				// show progress bar or something to the user while login is
				// happening
			//	mTextStatus.setText("Thinking...");
			}

			@Override
			public void onLogout() {
				// change the state of the button or do whatever you want
				//mTextStatus.setText("Logged out");
				loggedOutUIState();
			}

		};

		mButtonLogout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				mSimpleFacebook.logout(onLogoutListener);
			}
		});
		
	}
    
    private void loggedOutUIState() {
    	MyUser.profile=null;
		Intent i=new Intent(getActivity(), LoginActivity.class);
		 startActivity(i);
		 getActivity().finish();
	}
    private void UpdateProfileView() {

    	if(ProfileFacebook.profile!= null ){
    		profilePictureView.setProfileId(ProfileFacebook.profile.getId());
            
            userNameView.setText(ProfileFacebook.profile.getFirstName());
    	}
	}
}
